/*
 * Copyright (c) 2017 Solidwang. All Rights Reserved
 */
package com.solid4j.auth.controller;


import com.solid4j.auth.common.Constants;
import com.solid4j.auth.model.Role;
import com.solid4j.auth.model.User;
import com.solid4j.auth.page.Page;
import com.solid4j.auth.service.RoleService;
import com.solid4j.auth.service.UserService;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

/**
 * @author: solidwang
 * @date: 17/4/18
 */
@Controller
@RequestMapping("/auth/user")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private RoleService roleService;

    @RequestMapping("/list")
    public String list(User user, @RequestParam(defaultValue = "1", required = true) Integer currentPage,
                       Model model) {
        String passport = user.getPassport();
        StringBuilder condition = new StringBuilder("");
        if(!StringUtils.isEmpty(passport)) {
            condition.append("passport like '%" + passport.trim() + "%'");
        }
        Page<User> page = userService.getUserList(condition.toString(), currentPage, Constants.PAGE_SIZE, "");
        model.addAttribute("page", page);
        model.addAttribute("user", user);
        return "/user/list";
    }

    @RequestMapping("/redirect")
    public String redirect(@RequestParam(defaultValue = "save", required = true) String type, @RequestParam(value = "id", required = false) Integer id, Model model) {
        if ("save".equals(type)) {
            List<Role> roles = roleService.getRoles();
            model.addAttribute("roles", roles);
            return "/user/save";
        } else if ("update".equals(type)) {
            User user = userService.get(id);
            List<Role> roles = roleService.getRoles();
            Map<Role, Integer> rolesMap = new LinkedHashMap<Role, Integer>();
            Map<Integer, Integer> checkedMap = new LinkedHashMap<Integer, Integer>();
            if (roles != null && !roles.isEmpty()) {
                for (Role role : roles) {
                    rolesMap.put(role, 0);
                }
            }
            if (user.getRoles() != null && !user.getRoles().isEmpty()) {
                for (Role role : user.getRoles()) {
                    checkedMap.put(role.getId(), 1);
                }
            }
            model.addAttribute("user", user);
            model.addAttribute("rolesMap", rolesMap);
            model.addAttribute("checkedMap", checkedMap);
            return "/user/update";
        }
        return null;
    }

    @RequestMapping("/save")
    public String save(@ModelAttribute User user, @RequestParam(value = "", defaultValue = "") String roleIds) {
        Set<Role> roles = new LinkedHashSet<Role>();
        user.setCreateTime(new Date());
        user.setModifyTime(new Date());
        if (!"".equals(roleIds)) {
            String[] roleIdArr = roleIds.split(",");
            for (String roleId : roleIdArr) {
                roles.add(roleService.get(Integer.parseInt(roleId)));
            }
        }
        user.setRoles(roles);
        Integer id = userService.save(user);
        return "redirect:/auth/user/list.do?currentPage=1";
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam(value = "id") Integer id,
                         @RequestParam(value = "currentPage", defaultValue = "1") int currentPage) {
        User user = userService.get(id);
        userService.delete(user);
        return "redirect:/auth/user/list.do?currentPage=" + currentPage;
    }

    @RequestMapping("/update")
    public String update(@ModelAttribute User user, @RequestParam(value = "currentPage", defaultValue = "1") int currentPage, @RequestParam(value = "", defaultValue = "") String roleIds) {
        User newUser = userService.get(user.getId());
        Set<Role> roles = new LinkedHashSet<Role>();
        newUser.setMobile(user.getMobile());
        newUser.setModifyTime(new Date());
        if (!"".equals(roleIds)) {
            String[] roleIdArr = roleIds.split(",");
            for (String roleId : roleIdArr) {
                roles.add(roleService.get(Integer.parseInt(roleId)));
            }
        }
        newUser.setRoles(roles);
        userService.update(newUser);
        return "redirect:/auth/user/list.do?currentPage=" + currentPage;
    }

}
